import java.io.*;
import java.util.*;

public class JueCe {
    public static void main(String[] args) throws Exception {
       String s= question1("D:\\cuitstudy\\restart\\决策引擎Java-data.txt");
        String[] split = s.split("\n");
        String hour=split[1];
        String half=split[3];
        String quarter=split[5];
        System.out.println("半小时数据：");
        System.out.println(question2(hour,half));
        System.out.println("半小时排序数据：");
        sortMap(question2(hour,half));
        System.out.println("\n15分钟数据：");
        System.out.println(question3(hour,quarter));
        System.out.println("15分钟排序数据");
        sortMap(question3(hour,quarter));
    }
    //读取数据到String中
    public static String question1(String file) throws Exception {
        BufferedReader fis=new BufferedReader(new FileReader(file));
        String ch=null;
        StringBuffer sb=new StringBuffer();
        while((ch=fis.readLine())!=null){
            sb.append(ch+"\n");
        }
        System.out.print(sb.toString());
        return sb.toString();
    }
    //问题二
    public static HashMap<String,Integer> question2(String hour,String half){
        HashMap<String,Integer> map=new HashMap<>();
        String[] ho = hour.split(";");
        String[] ha = half.split(";");
        for (int i = 0; i <ho.length ; i++) {
            String[] split = ha[i].split(",");
            for (int j = 0; j < 2; j++) {
                if(j==0){
                    map.put("0"+ho[i]+"30",Integer.parseInt(split[j].split(":")[1]));
                }else {
                    map.put("0"+ho[i]+"60",Integer.parseInt(split[j].split(":")[1]));
                }
            }
        }

        return map;
    }
    //问题三
    public static HashMap<String,Integer> question3(String hour,String quarter){
        HashMap<String,Integer> map=new HashMap<>();
        String[] ho = hour.split(";");
        String[] qua = quarter.split(";");
        for (int i = 0; i <ho.length ; i++) {
            String[] split = qua[i].split(",");
            for (int j = 0; j < 4; j++) {
                if(j==0){
                    map.put("0"+ho[i]+"15",Integer.parseInt(split[j].split(":")[1]));
                }else if(j==1) {
                    map.put("0"+ho[i]+"30",Integer.parseInt(split[j].split(":")[1]));
                }else if(j==2) {
                    map.put("0"+ho[i]+"45",Integer.parseInt(split[j].split(":")[1]));
                }else {
                    map.put("0"+ho[i]+"60",Integer.parseInt(split[j].split(":")[1]));
                }
            }
        }

        return map;
    }
    //对map进行排序然后打印
    public static void sortMap(HashMap<String,Integer> map){

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        ArrayList<Map.Entry<String, Integer>> list=new ArrayList<>(map.entrySet());
        list.stream().sorted(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue()-o2.getValue();
            }
        }).forEach(s-> System.out.print("<"+s.getKey()+","+s.getValue()+"> "));
    }
}
